ExplicitEnum subclass str (JSON dump compatible) #17933
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I found that when I wanted to write the parsed dataclasses that I get from
HfArgumentParser.parse_args_into_dataclasses()to JSON, that I would get JSON errors. The reason being thatTypeError: Object of type IntervalStrategy is not JSON serializable. While this is understandable (Enum members are not serializable), this is not ideal withintransformers.I checked all items in
transformersthat subclassExplicitEnumand it seems that they are allstr-only Enums. That would allow us to have them inherit fromstr, too, which solves the JSON issue. JSON can then make use of itsstrclass for serialization. Below is a minimal - but full - example to show how this would work:A consequence is that now these ExplicitEnums will have a Union type, which originally lead to issues when using
HfArgumentParser._parse_dataclass_field. Therefore, I added an exception to_parse_dataclass_fieldto allow for a Union if one of the types isstr, assuming that a given string value to the argparser will be resolved correctly, because it is one of the accepted types.Who can review?
@sgugger